package net.xdclass.user.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.user.model.vo.UserVO;
import net.xdclass.user.request.UserLoginRequest;
import net.xdclass.user.request.UserRegisterRequest;
import net.xdclass.user.service.UserService;
import net.xdclass.common.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2021-01-26
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {

    @Autowired
    private UserService userService;
//    @Autowired
//    private FileHandlers fileHandlers;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public JsonData register(@RequestBody UserRegisterRequest request){
        return userService.register(request);
    }

    /**
     * @RequestPart 接收文件以及其他更为复杂的数据类型
     * @param file
     * @return
     */
    @ApiOperation("上传用户头像")
    @PostMapping("/headImg")
    public JsonData uploadHeadImg(@ApiParam(value = "用户头像文件", required = true) @RequestPart("file") MultipartFile file){
//        UploadResult result = fileHandlers.upload(file, AttachmentType.TENCENTCOS);
//        return JsonData.buildSuccess(result.getFilePath());
        return JsonData.buildSuccess(); // TODO
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonData login(HttpServletRequest request,
                          @ApiParam("用户登录对象") @RequestBody UserLoginRequest user){
        return userService.login(request, user);
    }

    /**
     * 用户个人信息查询
     * @return
     */
    @ApiOperation("查询用户信息")
    @GetMapping("/detail")
    public JsonData userDetail(){
        UserVO user = userService.selectCurrentUser();
        return JsonData.buildSuccess(user);
    }

}

